var usermoneyinfoManager;
var usermoneyinfoItemPageOpen;
var usermoneyinfoItem;

function initUserMoneyInfoManager(){
	var colimnsArray = [];
	
		colimnsArray.push({
			display : '用户名称',
			name : 'userInfoName',
			align : 'left',
			showTitle : true,
			minWidth : 50,
			align : "center",
			render:function(item){ return item.userInfo.name;}
		});
		colimnsArray.push({
			display : '身份证号码',
			name : 'userInfoCard',
			align : 'left',
			showTitle : true,
			minWidth : 50,
			align : "center",
			render:function(item){ return item.userInfo.cardId;}
		});
		colimnsArray.push({
			display : '充值金额',
			name : 'money',
			align : 'left',
			showTitle : true,
			minWidth : 50,
			align : "center",
			editor: { type: 'text'},
			isSort : false
		});
		colimnsArray.push({
			display : '渠道类型',
			name : 'qd',
			align : 'left',
			showTitle : true,
			minWidth : 50,
			align : "center",
			editor: { type: 'text'},
			isSort : false,
			editor: { type: 'select', data: qdRenderData, valueColumnName: 'value' },
			render:function(item){ return defaultRenderFun(item.qd,qdRenderData);}
		});
		colimnsArray.push({
			display : '渠道账户',
			name : 'qdUser',
			align : 'left',
			showTitle : true,
			minWidth : 50,
			align : "center",
			editor: { type: 'text'},
			isSort : false
		});
		colimnsArray.push({
			display : '充值状态',
			name : 'state',
			align : 'left',
			showTitle : true,
			minWidth : 50,
			align : "center",
			editor: { type: 'text'},
			isSort : false,
			editor: { type: 'select', data: payStateRenderData, valueColumnName: 'value' },
			render:function(item){ return defaultRenderFun(item.state,payStateRenderData);}
		});
		colimnsArray.push({
			display : '创建时间',
			name : 'registTime',
			align : 'left',
			showTitle : true,
			minWidth : 50,
			align : "center",
			type:'date',
			format : 'yyyy-MM-dd hh:mm:ss',
			render : dateRenderFun,
			editor: { type: 'date'},
			isSort : false
		});
		colimnsArray.push({
			display : '成功时间',
			name : 'configTime',
			align : 'left',
			showTitle : true,
			minWidth : 50,
			align : "center",
			type:'date',
			format : 'yyyy-MM-dd hh:mm:ss',
			render : dateRenderFun,
			editor: { type: 'date'},
			isSort : false
		});
	
	var menu = $.ligerMenu({ width: 120, items:
		[
			{ text: '增加', click: function(){
				jQuery.ligerDialog.prompt('输入身份证号',function (yes,value) {
					if(yes)
						openUserMoneyInfoItemPage('create',"新增",{cardId:value});
					});
				}, icon: 'add' },
			{ line: true },
			{ text: '修改', click: function(){openUserMoneyInfoItemPage('edit',"编辑")} },
			{ line: true },
			{ text: '查看', click: function(){openUserMoneyInfoItemPage('show',"查看")} }
		]
	}); 
	
	return window['g'] = jQuery("#maingrid").ligerGrid({
		height : '100%',
		url:'/admin/user/userMoneyInfo/ajaxList.do',
		columns : colimnsArray,
		pageSize : 10,
		rownumbers : true,
		toolbar : {
			items : [ {
				text : '保存',
				click : saveUpdateUserMoneyInfoManager,
				icon : 'modify'
			}, {
				line : true
			}, {
				text : '增加',
				click : function(){
					jQuery.ligerDialog.prompt('输入身份证号',function (yes,value) {
						if(yes)
							openUserMoneyInfoItemPage('create',"新增",{cardId:value});
						});
					},
				icon : 'add'
			}, {
				line : true
			}]
		},
		onContextmenu : function (parm,e)
        {
			usermoneyinfoItem = parm.data;
	        menu.show({ top: e.pageY, left: e.pageX });
	        return false;
        },
        enabledEdit: false, 
        isScroll: false, 
        rownumbers:true
	});
};

function saveUpdateUserMoneyInfoManager(item){
	var data = usermoneyinfoManager.getUpdated();
	if(data.length == 0){
		$.ligerDialog.warn("您还未做任何修改哦!");
		return;
	}
	doSaveUserMoneyInfos(data);
};
function doSaveUserMoneyInfos(newData){
	jQuery.ajax({
		url:'/admin/user/userMoneyInfo/saveOrUpdate.do',
		data:{json:JSON.stringify(newData)},
		dataType:'json',
		type:'post',
		success:function(data){
			if(data.sucessList.length > 0){
				$.ligerDialog.success('恭喜,已经保存成功!');
				tip = $.ligerDialog.tip({ title: '提示信息', content: '恭喜,已经保存'+data.sucessList.length+'条记录!' });
				usermoneyinfoManager.loadServerData({pagesize:10});
				usermoneyinfoItemPageOpen.hidden();
			}else{
				if(data.msg){
					tip = $.ligerDialog.tip({ title: '提示信息', content: data.msg });
				}else{
					tip = $.ligerDialog.tip({ title: '提示信息', content: '网络异常,没有保存成功!' });
				}
				
			}
		},
		error:function(){
			$.ligerDialog.error("网络发生异常,请稍后再试!");
		}
	});
};
/**
 * 
 * @param item
 * @param target
 */
function openUserMoneyInfoItemPage(target,titleName,optionJson){
	/*var vrl = '/admin/user/userMoneyInfo/'+target+'.do';
	if(usermoneyinfoItem && usermoneyinfoItem.id){
		vrl += '?id='+usermoneyinfoItem.id;
	}
	usermoneyinfoItemPageOpen = $.ligerDialog.open({ url:vrl,height:400,isResize:true,title:titleName,width:450 });*/
	if(!optionJson){
		optionJson = {};
	}
	optionJson.id = ((usermoneyinfoItem&&usermoneyinfoItem.id)?usermoneyinfoItem.id:0);
	jQuery.ajax({
		url:'/admin/user/userMoneyInfo/'+target+'.do',
		data:optionJson,
		dataType:'text',
		type:'post',
		success:function(data){
			if(data){
				jQuery("#openDiv").html(data);
				usermoneyinfoItemPageOpen = $.ligerDialog.open({ target: $("#openDiv"),isResize:true,title:titleName,width:450 });
			}
		},
		error:function(){
			$.ligerDialog.error("网络发生异常,请稍后再试!");
		}
	});
};

jQuery(function() {
	usermoneyinfoManager = initUserMoneyInfoManager();
    jQuery("#pageloading").hide();
});